var fs = require('fs');
var myArgs = process.argv.slice(1);


var content = fs.readFileSync(myArgs[1], 'utf8');
 
var nbrContact = 0, answer;
var contact = [], nom = [], tel = [], adresse = [];
var vCardsChoix = [], vCardsNope = [], vCardsToDelete = [];
var deleteThisOne = false;

for ( var i = 1; i <= content.length; i++){
    var fin = content.indexOf("END:VCARD")+9;
    contact[i] = content.substring(0, fin);
    content = content.replace(contact[i], "");
    nbrContact++;
}

for (i = 1; i <= nbrContact; i++){
    posNom = contact[i].indexOf("FN:") + 3;
    posFinNom = contact[i].indexOf("TEL;") - 1;
    nom[i] = contact[i].substring(posNom, posFinNom);
   
    posTel1 = contact[i].indexOf("CELL:") + 5;
    posTel1Fin = contact[i].indexOf("EMAIL") - 1;
    tel[i] = contact[i].substring(posTel1, posTel1Fin);
    tel[i] = tel[i].replace("HOME:","");
    tel[i] = tel[i].replace("PREF:","");
   
    posAdresse = contact[i].indexOf("EMAIL") + 6;
    posAdresseFin = contact[i].indexOf("END:VCARD") - 1;
   
    adresse[i] = contact[i].substring(posAdresse, posAdresseFin);
    adresse[i] = adresse[i].replace("HOME:","");
    adresse[i] = adresse[i].replace("PREF:","");
   
}

for (i = 1;i <= nbrContact ;i++)
{
        if (!vCardsNope[i])
        {
                var vCardsIdentiques,isIdentiques=false;
                for (j=nbrContact;j>i;j--)
                {      
                        if ( nom[i] == nom[j] )
                        {
                                if (!isIdentiques)
                                {
                                        isIdentiques = true;
                                        vCardsIdentiques = (";"+j);
                                                                       
                                } else {
                                        vCardsIdentiques = vCardsIdentiques.concat((";"+j).toString());
                                }
                                vCardsNope[j]=true;;
                        }
                }
                if (isIdentiques)
                {
                        vCardsIdentiques = (i).toString().concat(vCardsIdentiques);
                        vCardsChoix.push(vCardsIdentiques);
                }
        }
}

console.log(vCardsChoix);

var readline = require('readline');

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
console.log("Demande d'intervention");
   

console.log(vCardsChoix[i]);

rl.question("Saisir les contacts à supprimer ",function(answer) {
        console.log(answer);
        vCardsToDelete = answer.split(";");
        var listContact = "";
               
        console.log("Debut suppression");
        for (i = 1 ; i <= nbrContact; i++) {
                for (j = 0; j < vCardsToDelete.length; j++)
                {
                        if(vCardsToDelete[j] == i)
                        {
                                deleteThisOne = true;
                        }
                }
                if (!deleteThisOne)
                {
                        listContact += contact[i];
                }
                deleteThisOne = false;
        }
        console.log("Fin suppression");

        fs.writeFile( myArgs[1], listContact , function (err) {
                if (err) throw err;
                console.log('Traitement terminé ');
                 
        });
       
        fs.appendFile('vcard.vcf','\r\n');
               
        setTimeout(rl.close(), 10000);
        });
